package com.kch.web.controller.system;

import com.kch.common.annotation.Log;
import com.kch.common.core.controller.BaseController;
import com.kch.common.core.domain.AjaxResult;
import com.kch.common.core.page.TableDataInfo;
import com.kch.common.enums.BusinessType;
import com.kch.framework.util.ShiroUtils;
import com.kch.system.domain.SysBanner;
import com.kch.system.domain.SysCloumn;
import com.kch.system.service.ISysBannerService;
import com.kch.system.service.ISysCloumnService;
import com.kch.web.config.CloumnProperties;
import com.kch.web.controller.common.CheckNumberUtils;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/system/banner")
@Api(tags = "banner管理")
public class SysBannerController extends BaseController {

    private String prefix = "system/banner";
    @Autowired
    private ISysBannerService bannerService;
    @Autowired
    private ISysCloumnService sysCloumnService;
    @Autowired
    private CloumnProperties cloumnProperties;

    @RequiresPermissions("system:banner:view")
    @GetMapping()
    public String index(ModelMap mmap) {
        List<SysCloumn> collect = new ArrayList<>();
        collect.add(sysCloumnService.selectByCode(cloumnProperties.getDbanner()));
        collect.add(sysCloumnService.selectByCode(cloumnProperties.getHbanner()));
        mmap.put("cols", collect);
        return prefix + "/banner";
    }

    @PostMapping("/list")
    @RequiresPermissions("system:banner:list")
    @ResponseBody
    public TableDataInfo list(SysBanner banner) {
        startPage();
        List<SysBanner> list = bannerService.selectMeetingPage(banner);
        return getDataTable(list);
    }

    @GetMapping("/add")
    public String add(ModelMap mmap) {
        List<SysCloumn> collect = new ArrayList<>();
        collect.add(sysCloumnService.selectByCode(cloumnProperties.getDbanner()));
        collect.add(sysCloumnService.selectByCode(cloumnProperties.getHbanner()));
        mmap.put("cols", collect);
        return prefix + "/add";
    }

    @Log(title = "banner", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@Validated SysBanner sysBanner) {
        sysBanner.setCreateBy(ShiroUtils.getLoginName());
        if(!ObjectUtils.isEmpty(sysBanner.getWeight())){
            if(!CheckNumberUtils.check(String.valueOf(sysBanner.getWeight()))){
                return AjaxResult.error("权重数据必须为整型");
            }
        }
        bannerService.insertSysBanner(sysBanner);
        return toAjax(true);
    }

    @Log(title = "banner", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(bannerService.deleteSysBanner(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

}
